import { Controller, Get, Param } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Controller('qq/xsyj')
export class XsyjController {
    constructor(
        private readonly prismaService: PrismaService
    ) { }

    @Get(":unionid/get")
    async get(
        @Param("unionid") unionid: string
    ) {
        const data = await this.prismaService.xsyj.findFirst({
            where: {
                unionid
            },
            include: {
                xsyjCheck: true
            }
        })
        if (!data) return [];
        return (data.xsyjCheck || []).map(e => e.ans);
    }

    @Get(":unionid/:value/:ans/set")
    async set(
        @Param("unionid") unionid: string,
        @Param("value") value: string,
        @Param("ans") ans: any
    ) {
        ans = parseInt(ans);
        let data = await this.prismaService.xsyj.findFirst({
            where: {
                unionid
            }
        })
        if (!data) {
            await this.prismaService.xsyj.create({
                data: {
                    unionid,
                    value,
                    xsyjCheck: {
                        create: {
                            ans
                        }
                    }
                }
            })
            return;
        }
        await this.prismaService.xsyjCheck.create({
            data: {
                ans,
                target: {
                    connect: {
                        id: data.id
                    }
                }
            }
        })
    }
}
